package mvp.com.bbc.rcjavaretrofitdaggermvp.baseAF;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import butterknife.ButterKnife;
import mvp.com.bbc.rcjavaretrofitdaggermvp.R;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.utils.ActivityUtils;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.utils.StatusBarUtil;


/**
 * Created by 轻吻旧时光 on 2017/4/26.
 */

public abstract class BaseEmptyActivity extends Activity implements View.OnClickListener {

    public ArrayList<LinearLayout> layouts = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置竖屏
        statusBarSetting();
        ButterKnife.bind(this);//注解
        ActivityUtils.add(this);
        findViews();
        setListeners();
        init();

        //注册EventBus
        EventBus.getDefault().register(this);
        //统计模式  普通模式
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
    }

    /**
     * 绑定id
     */
    protected abstract void findViews();

    /**
     * 初始化操作
     */
    protected abstract void init();

    /**
     * 事件监听
     */
    protected abstract void setListeners();

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityUtils.remove(this);
        //注销EventBus
        EventBus.getDefault().unregister(this);
    }

    /**
     * EventBus接收器
     *
     * @param action
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String action) {

    }

    /**
     * 手机状态栏设置
     */
    private void statusBarSetting() {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }

    /**
     * 初始化标题栏
     *
     * @param title_bar 标题栏布局
     * @param title     标题名称
     * @param type      是否是图标
     * @param property  标题栏右角标个数与属性
     */
    public void initTitleBar(LinearLayout title_bar, String title, boolean type, String[] property) {
        StatusBarUtil.setNaviBarPaddingTop(this, title_bar);
        TextView title_id = (TextView) title_bar.findViewById(R.id.homepage_live_title);
        title_id.setText(title);

        LinearLayout title_bar_corner = (LinearLayout) title_bar.findViewById(R.id.title_bar_corner);
        LinearLayout break_off = (LinearLayout) title_bar.findViewById(R.id.back_off);
        break_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.break_off();
            }
        });

        if (property == null) {
            return;
        }
        if (type) {
            layouts = getCornerImg(property);
            for (int i = 0; i < layouts.size(); i++)
                title_bar_corner.addView(layouts.get(i));
        } else {
            layouts = getCornerText(property);
            for (int i = 0; i < layouts.size(); i++)
                title_bar_corner.addView(layouts.get(i));
        }
    }

    private ArrayList<LinearLayout> getCornerText(String[] property) {
        ArrayList<LinearLayout> layouts = new ArrayList<>();

        for (int i = 0; i < property.length; i++) {
            LinearLayout corner = (LinearLayout) this.getLayoutInflater().inflate(R.layout.title_bar_corner_text, null);
            TextView textView = (TextView) corner.findViewById(R.id.corner_text);
            if (property[i].equals("保存")) {
                textView.setText("保存");
            }
            layouts.add(corner);
        }

        return layouts;
    }

    public ArrayList<LinearLayout> getCornerImg(String[] property) {

        ArrayList<LinearLayout> layouts = new ArrayList<>();

        for (int i = 0; i < property.length; i++) {
            LinearLayout corner = (LinearLayout) this.getLayoutInflater().inflate(R.layout.title_bar_corner, null);
            ImageView imageView = (ImageView) corner.findViewById(R.id.corner_img);
            if (property[i].equals("搜索")) {

                imageView.setImageResource(R.mipmap.search);

            }
            layouts.add(corner);
        }

        return layouts;
    }

}
